﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using CSRedis;
using Mes.Pendant.ArrivalNotices;
using Mes.Pendant.ArrivalNotices.Dto;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialManagements;
using Mes.Pendant.MaterialManagementService.Dto;
using Mes.Pendant.NotificationArrivals;
using Mes.Pendant.SupplierManagements;
using Mes.Pendant.Warehousings;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Mes.Pendant.NotificationArrivalServicess
{
	/// <summary>
	/// 到货通知
	/// </summary>
	public class ArrivalNoticeServices : ApplicationService, IArrivalNoticeServices
    {
        /// <summary>
        /// 到货通知仓储
        /// </summary>
        private readonly IRepository<NotificationArrival, int> notificationArrivalRepository;
        /// <summary>
        /// 产品入库仓储
        /// </summary>
        private readonly IRepository<Warehousing, int> warehouseRepository;
        /// <summary>
        /// 供应商管理仓储
        /// </summary>
        private readonly IRepository<SupplierManagement, int> supplierManagementRepository;

        private static Object obj=new Object();
        private readonly CSRedisClient cSRedisClient = new CSRedisClient("localhost:6379");
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="notificationArrivalRepository">到货通知仓储</param>
        /// <param name="warehouseRepository">产品入库仓储</param>
        public ArrivalNoticeServices(IRepository<NotificationArrival, int> notificationArrivalRepository, IRepository<Warehousing, int> warehouseRepository, IRepository<SupplierManagement, int> supplierManagementRepository)
        {
            this.notificationArrivalRepository = notificationArrivalRepository;
            this.warehouseRepository = warehouseRepository;
            this.supplierManagementRepository = supplierManagementRepository;
        }

        /// <summary>
        /// 获取到货通知数据
        /// </summary>
        /// <param name="inboundListCode"></param>
        /// <param name="notifiName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetArrivalDatas(string? inboundListCode, string? notifiName, int index = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<NotificationArrival>(true);
            if (!string.IsNullOrEmpty(inboundListCode))
            {
                predicate = predicate.And(m => m.InboundListCode.Contains(inboundListCode));
            }
            if (!string.IsNullOrEmpty(notifiName))
            {
                predicate = predicate.And(m => m.NotifiName.Contains(notifiName));
            }
            var arrivalList = notificationArrivalRepository.GetListAsync(predicate).Result;
            PagedResult pagedResult = new PagedResult();
            pagedResult.CurrentPage = index;
            pagedResult.PageSize = limit;
            pagedResult.RowCount = arrivalList.Count();
            pagedResult.PageCount = (int)Math.Ceiling((pagedResult.RowCount) / (double)limit);
            pagedResult.Queryable = arrivalList.OrderBy(x => x.Id).Skip((index - 1) * limit).Take(limit).AsQueryable();
            pagedResult.Queryable = pagedResult.Queryable.MapToList<SearchArrivalNotices>().AsQueryable();
            return pagedResult;
        }

        /// <summary>
        /// 获取供应商数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<Suppierlistdto>> Getsuppernames()
        {
            var list = supplierManagementRepository.GetListAsync().Result.AsQueryable();
            var supperdto = list.MapToList<Suppierlistdto>();
            return supperdto;
        }

        /// <summary>
        /// redis绑定供应商
        /// </summary>
        /// <returns></returns>
        public async Task<List<SupplierManagement>> GetSupperlist()
        {
            var supperList = cSRedisClient.Get<List<SupplierManagement>>("supperkey");
            if (supperList == null)
            {
                lock (obj)
                {
                    if(supperList == null)
                    {
                        supperList = supplierManagementRepository.GetListAsync().Result;
                        var seconds = 3000 + new Random().Next(1, 1000);
                        cSRedisClient.Set("supperkey", supperList, seconds);
                    }
                }
            }
            return supperList;
        }

        /// <summary>
        /// 添加到货通知
        /// </summary>
        /// <param name="materialmangement"></param>
        /// <returns></returns>
        public async Task<int> AddNotifsals(InsertNotifidatadto insertNotifidatadto)
        {
            var notificationArrival = insertNotifidatadto.MapTo<NotificationArrival>();
            await notificationArrivalRepository.InsertAsync(notificationArrival);
            return 1;
        }

        /// <summary>
        /// 删除到货通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteNotifidatas(int id)
        {
            await notificationArrivalRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 获取到货通知根据id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UpdateNotifdatadto> SelectByNotis(int id)
        {
            var entity = notificationArrivalRepository.GetAsync(id).Result;
            var notidto= entity.MapTo<UpdateNotifdatadto>();
            return notidto;
        }

        /// <summary>
        /// 修改到货通知
        /// </summary>
        /// <param name="updateNotifdatadto"></param>
        /// <returns></returns>
        public async Task<int> UpdateArrivals(UpdateNotifdatadto updateNotifdatadto)
        {
            var entity = ObjectMapper.Map<UpdateNotifdatadto, NotificationArrival>(updateNotifdatadto);
            await notificationArrivalRepository.UpdateAsync(entity);
            return 1;
        }
    }
}
